//
//  Login.m
//  百车易车险
//
//  Created bychenxu on 14-7-31.
//  Copyright (c) 2014年 chenxu. All rights reserved.
//

#import "Login.h"
#import "AppDelegate.h"
#import "NSObject+ObjectMap.h"
#import "BPush.h"
#import "NetBaiduManager.h"
#import "NetAPIManager.h"
#define kLoginStatus @"login_status"
#define kLoginUserId @"user_id"
#define kLoginUserDict @"user_dict"
#define kSelectCar @"car_select"




static User *curLoginUser;  //当前登录的用户
static Car  *curCar;
static NSString *curCid;    //标记当前选择的车id
static NSString *curCarid;  //标记当前选择的车牌号码

@interface Login  ()

@end

@implementation Login

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.remember_me = [NSNumber numberWithBool:YES];
        self.username = @"";
        self.password = @"";
    }
    return self;
}

/*!
 *  @brief  用字典方式返回所有参数
 *  @return 参数字典
 */
- (NSDictionary *)toParams{

        return @{@"username" : self.username,
                 @"password" : self.password,
                 @"user_id" : [BPush getUserId],
                 @"channel_id" : [BPush getChannelId],
                 @"device" : @"2"
                 };
    
}

- (NSDictionary *)toUpdateidParams{
    return @{@"username" : self.username,
             @"user_id" : [BPush getUserId],
             @"channel_id" : [BPush getChannelId],
             };
}
/*!
 *  @brief  本地更新密码、个人信息、注册的时候需要更新单例curUser
 *  @param newuser 新用户
 */
+ (void)updateUser:(User*)newuser{
    curLoginUser = newuser;
}

/*!
 *  @brief  根据填写内容进行验证
 *  @param needCaptcha 需要验证码
 *  @return 提示信息
 */
- (NSString *)goToLoginTipWithCaptcha:(BOOL)needCaptcha{
    if (!_username || _username.length <= 0) {
        return @"请填写电子邮箱或个性后缀";
    }
    if (!_password || _password.length <= 0) {
        return @"请填写密码";
    }
    if (needCaptcha && (!_j_captcha || _j_captcha.length <= 0)) {
        return @"请填写验证码";
    }
    return nil;
}

/*!
 *  @brief  从持久化层获取是否已经登陆
 *  @return 是/否
 */
+ (BOOL)isLogin{
    NSNumber *loginStatus = [[NSUserDefaults standardUserDefaults] objectForKey:kLoginStatus];
    if (loginStatus.boolValue && [Login curLoginUser]) {
        return YES;
    }else{
        return NO;
    }
}

/*!
 *  @brief  保存用户信息
 *  @param loginData 用户的参数字典
 */
+ (void)doLogin:(NSDictionary *)loginData{
    if (loginData) {
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        //持久化
        [defaults setObject:[NSNumber numberWithBool:YES] forKey:kLoginStatus];
        
        NSMutableDictionary* temp = [[NSMutableDictionary alloc]initWithDictionary:loginData];
        
        
        [self cleanDictionary:temp];
        //temp = [self lowercase:temp];
       
        [defaults setObject:temp forKey:kLoginUserDict];
        [defaults synchronize];

         curLoginUser = [NSObject objectOfClass:@"User" fromJSON:temp];
        
        
        //SDK
      //  [Login addUmengAliasWithCurUser:YES];
      //  [Login setXGAccountWithCurUser];
    }else{
        [Login doLogout];
    }
}

/*!
 *  @brief  清除里面的null换成@""
 *  @param dictionary 待处理的
 */
+ (void)cleanDictionary:(NSMutableDictionary *)dictionary {
    [dictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
        if (obj == [NSNull null]) {
            [dictionary setValue:@"" forKey:key];
        } else if ([obj isKindOfClass:[NSDictionary class]]) {
            [self cleanDictionary:obj];
        }
    }];

}

+ (NSMutableDictionary*)lowercase:(NSMutableDictionary*)dict{
    NSMutableDictionary* newdict =[[NSMutableDictionary alloc]init];
    
    for (NSString* key in dict) {
        NSString* newkey = [key lowercaseString];
        [newdict setValue:[dict valueForKey:key] forKey:newkey];
    }
    return newdict;
}


/*!
 *  @brief  注销登陆
 */
+ (void)doLogout{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:[NSNumber numberWithBool:NO] forKey:kLoginStatus];
    [defaults synchronize];
}

/*!
 *  @brief  返回现在登陆的用户
 *  @return User
 */
+ (User *)curLoginUser{
    if (!curLoginUser) {
        NSDictionary *loginData = [[NSUserDefaults standardUserDefaults] objectForKey:kLoginUserDict];
        curLoginUser = loginData? [NSObject objectOfClass:@"User" fromJSON:loginData]: nil;
    }
    return curLoginUser;
}


/*!
 *  @brief  判断是不是同一个user
 *  @param owner_id userid
 *  @return 是/否
 */
+ (BOOL)isOwnerOfProjectWithOwnerId:(NSNumber *)owner_id{
    User *curLoginUser = [Login curLoginUser];
    if (curLoginUser) {
        return YES;
       // return (curLoginUser.id.integerValue == owner_id.integerValue);
    }else{
        return NO;
    }
}


+ (void)selectCar:(Car *)car;{
    if(car){
        curCar = car;
        NSString *tip = @"您选择了:";
        tip  = [tip stringByAppendingString:curCar.Carid];
        [self showHudTipStr:tip];
    }
}

+ (Car*)curCar{
    if(curCar){
        return curCar;
    }
    else{
        return nil;
    }
}

+ (NSString *)curCid{
    if (curCar) {
        return curCar.Cid;
    }
    else{
        return @"";
    }
}

+ (NSString *)curCarid{
    if (curCar) {
        return curCar.Carid;
    }
    else{
        return @"车牌号";
    }
}







@end
